package com.hww.common.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hww.common.R;
import com.hww.common.util.ActivityUtils;
import com.hww.common.util.ThreadUtils;

/**
 * @author hww
 * @date 0010 2024/7/10
 * email: heww@zhongyuntech.cn
 * description:
 */
public class CommonDialog extends BaseDialog {


    /**
     * 显示的图片
     */
    private ImageView imageIv;

    /**
     * 显示的标题
     */
    private TextView titleTv;

    /**
     * 显示的消息
     */
    private TextView messageTv;

    /**
     * 确认和取消按钮
     */
    private Button negativeBn, positiveBn;
    /**
     * 输入框
     */
    private EditText editText;

    /**
     * 按钮之间的分割线
     */
    private View columnLineView;

    /**
     * 都是内容数据
     */
    private String message;
    private String title;
    private String positive, negative;
    private int imageResId = -1;

    /**
     * 底部是否只有一个按钮
     */
    private boolean isSingle = false;

    public CommonDialog(@NonNull Context context) {
        super(context, R.style.CustomDialog);
    }

    public CommonDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }


    @Override
    public int bindLayout() {
        return R.layout.common_dialog_layout;
    }

    @Override
    public void initView(BaseDialog dialog, View contentView) {
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //初始化界面控件
        negativeBn = (Button) contentView.findViewById(R.id.dialog_common_negative);
        positiveBn = (Button) contentView.findViewById(R.id.dialog_common_positive);
        titleTv = (TextView) contentView.findViewById(R.id.dialog_common_title);
        messageTv = (TextView) contentView.findViewById(R.id.dialog_common_message);
        imageIv = (ImageView) contentView.findViewById(R.id.dialog_common_image);
        editText = (EditText) contentView.findViewById(R.id.dialog_common_edit);
        columnLineView = contentView.findViewById(R.id.dialog_common_column_line);
        editText.setVisibility(View.GONE);
        //初始化界面数据
        refreshView();
        //初始化界面控件的事件
        initEvent();
    }

    @Override
    public void setWindowStyle(Window window) {

    }


    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        positiveBn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickBottomListener != null) {
                    onClickBottomListener.onPositiveClick();
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        negativeBn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickBottomListener != null) {
                    onClickBottomListener.onNegativeClick();
                }
                dismiss();
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void refreshView() {
        //如果用户自定了title和message
        if (!TextUtils.isEmpty(title)) {
            titleTv.setText(title);
            titleTv.setVisibility(View.VISIBLE);
        } else {
            titleTv.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(message)) {
            messageTv.setText(message);
            messageTv.setVisibility(View.VISIBLE);
        } else {
            messageTv.setVisibility(View.GONE);
        }
        //如果设置按钮的文字
        if (!TextUtils.isEmpty(positive)) {
            positiveBn.setText(positive);
        } else {
            positiveBn.setText("确定");
        }
        if (!TextUtils.isEmpty(negative)) {
            negativeBn.setText(negative);
        } else {
            negativeBn.setText("取消");
        }

        if (imageResId != -1) {
            imageIv.setImageResource(imageResId);
            imageIv.setVisibility(View.VISIBLE);
        } else {
            imageIv.setVisibility(View.GONE);
        }
        /**
         * 只显示一个按钮的时候隐藏取消按钮，回掉只执行确定的事件
         */
        if (isSingle) {
            columnLineView.setVisibility(View.GONE);
            negativeBn.setVisibility(View.GONE);
        } else {
            negativeBn.setVisibility(View.VISIBLE);
            columnLineView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void show() {
        ThreadUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (ActivityUtils.isActivityAlive((getContext()))) {
                    refreshView();
                    CommonDialog.super.show();
                }
            }
        });
    }

    /**
     * 设置在底部显示
     */
    public void showBottom() {
        getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        WindowManager windowManager = mActivity.getWindowManager();
        /*Display defaultDisplay = windowManager.getDefaultDisplay();*/
        WindowManager.LayoutParams attributes = this.getWindow().getAttributes();
        attributes.gravity = Gravity.BOTTOM;
        this.getWindow().setAttributes(attributes);
        show();
    }

    /**
     * 设置确定取消按钮的回调
     */
    public OnClickBottomListener onClickBottomListener;

    public CommonDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
        this.onClickBottomListener = onClickBottomListener;
        return this;
    }

    public interface OnClickBottomListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick();

        /**
         * 点击取消按钮事件
         */
        public void onNegativeClick();
    }

    /**
     * 设置是否开启输入框
     *
     * @param visibility true显示，false隐藏
     * @return this
     */
    public CommonDialog setEditTxtVisibility(boolean visibility) {
        if (visibility) {
            editText.setVisibility(View.VISIBLE);
        } else {
            editText.setVisibility(View.GONE);
        }
        return this;
    }

    public EditText getEditText() {
        return editText;
    }

    public String getMessage() {
        return message;
    }

    public CommonDialog setMessage(String message) {
        this.message = message;
        return this;
    }

    public String getTitle() {
        return title;
    }

    public CommonDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public String getPositive() {
        return positive;
    }

    public CommonDialog setPositive(String positive) {
        this.positive = positive;
        return this;
    }

    public String getNegative() {
        return negative;
    }

    public CommonDialog setNegative(String negative) {
        this.negative = negative;
        return this;
    }

    public int getImageResId() {
        return imageResId;
    }

    public boolean isSingle() {
        return isSingle;
    }

    public CommonDialog setSingle(boolean single) {
        isSingle = single;
        return this;
    }

    public CommonDialog setImageResId(int imageResId) {
        this.imageResId = imageResId;
        return this;
    }
}
